#-*- encoding: utf-8 -*-
# Written by koya
import pygame
from pygame.locals import *

import sys, os, math
from effect import *
from message import *

from gui import *



def main():
    particles = pygame.sprite.Group()
    Particle.containers = particles

    sparks = pygame.sprite.Group()
    Spark.containers = sparks

    explositions = pygame.sprite.Group()
    Explosition.containers = explositions

    
    Message.containers = sparks



    
    pygame.init()
    window = pygame.display.set_mode((512,640))
    title = "大话梦游"
    pygame.display.set_caption(title.decode('utf-8').encode('cp936'))
    screen = pygame.display.get_surface()

    background =  pygame.Surface((512, 640))
    background.fill([0,0,0])
    screen.blit(background,[0,0])
    pygame.display.flip()

    clock = pygame.time.Clock()

    player_keyboard = {'Up' : K_w, 'Down' : K_s, 'Left' : K_a, 'Right' : K_d, 'Shoot' : K_LCTRL,'Bomb' : K_LSHIFT }
    forbid_key = [K_NUMLOCK, K_CAPSLOCK, K_SCROLLOCK, K_ESCAPE]
    # test user gui

    player1gui = UserGUI( 'Player1', 100, 100, 512, 640 )
    player2gui = UserGUI( 'Player2', 100, 100, 512, 640 )
    
    state = True
    while state:

        clock.tick(60)  
        screen.fill((0,0,0,))  
        keystate = pygame.key.get_pressed()
        events = pygame.event.get()

        if keystate[K_SPACE]:
            Spark((256,320), 'Image', 20,4,'Small Asteroid.PNG', True)
            
        if keystate[K_f]:
            Particle((200,200),'Ellipse', 4, None, True)
        
        if keystate[K_l]:
            Particle((300,200),'Line', 4, None, True)

        if keystate[K_i]:
            Particle((200,200),'Image', 4, 'Small Asteroid.PNG', True)


        if keystate[K_e]:
            Explosition(  (200,200), (255,0,0), 1000 )

        if keystate[K_z]:

            Message('I\'m koya!', (256,320), 20)
            

        if keystate[K_t]:
            player1gui.set_player_info( 50, 300, 50,  300, 100, 2 )
            player2gui.set_player_info( 50, 300, 50,  300, 100, 2 )

        
        
        #index = 0
        #for key in keystate:
            #if key:
                #if not find_in_list( index, forbid_key ):
                    #player_keyboard['Up'] = index
                    #print index 
            #index += 1
                
        #print player_keyboard

        player1gui.update()
        player1gui.render( screen )
        player2gui.update()
        player2gui.render( screen )

        for particle in particles:
            particle.update()
            particle.draw( screen )
            
        for spark in sparks:
            spark.update()
            spark.draw( screen )
            
        for explosition in explositions:
            explosition.update()
            explosition.draw( screen )
            
        pygame.display.flip()

        for event in events:
            if event.type  == QUIT or keystate[K_ESCAPE]:
                pygame.quit()
                sys.exit()



if __name__ == '__main__': main()

